package pt.ipleiria.estg.es2.byinvitationonly;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import pt.ipleiria.estg.es2.byinvitationonly.modelo.Contacto;


public class MainActivity extends Activity {

    private boolean isChecked = false;
    private MenuItem item;
    private Contacto contacto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        obterContactoUtilizador();
    }

    private void obterContactoUtilizador() {
        contacto = new Contacto("Marco",
                "mpmf.estg@gmail.com");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        item = menu.findItem(R.id.action_i_am_here);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_i_am_here) {
            invertCheckIn(item);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void invertCheckIn(final MenuItem item) {
        if (isChecked == false) {
            if (this.contacto != null && !contacto.getEmail().isEmpty()
                    && !contacto.getNome().isEmpty()) {

                AlertDialog.Builder construtor =
                        new AlertDialog.Builder(this);

                construtor.setTitle(getString(R.string.confirmacao))
                        .setMessage(getString(R.string.tem_a_certeza) +
                                getString(R.string.nome) + contacto.getNome() + getString(R.string.email) + contacto.getEmail())
                        .setNegativeButton(getString(R.string.nao), null)
                        .setPositiveButton(getString(R.string.sim), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (temRede()) {
                                    isChecked = true;
                                    item.setIcon(R.drawable.ic_action_group);
                                    Intent intent = new Intent(MainActivity.this, WhoIsHereActivity.class);
                                    intent.putExtra(WhoIsHereActivity.IS_CHECKED_IN, isChecked);
                                    startActivity(intent);
                                } else {
                                    Toast.makeText(MainActivity.this, getString(R.string.sem_rede),
                                            Toast.LENGTH_LONG).show();
                                }
                            }
                        });

                construtor.create().show();

            } else {
                Toast.makeText(this, getString(R.string.dados_incompletos),
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            isChecked = false;
            item.setIcon(R.drawable.ic_action_alone);
        }
    }

    private boolean temRede() {
        return true;
    }

    public MenuItem getItem() {
        return item;
    }
}
